#pragma once
#include "HashTable.h"

namespace ape
{
	template <class K, class Hash = HashFunc<K>>
	class unordered_set
	{
	public:
		// 获得key
		struct SetGetKey
		{
			const K &operator()(const K &key)
			{
				return key;
			}
		};

	private:
		ChainAddressing::HashTable<K, K, SetGetKey, Hash> _ht;

	public:
		typedef typename ChainAddressing::HashTable<K, K, SetGetKey, Hash>::const_iterator iterator;
		typedef typename ChainAddressing::HashTable<K, K, SetGetKey, Hash>::const_iterator const_iterator;

		// 迭代器
		iterator begin()
		{
			return _ht.begin();
		}

		iterator end()
		{
			return _ht.end();
		}

		const_iterator begin() const
		{
			return _ht.begin();
		}

		const_iterator end() const
		{
			return _ht.end();
		}

		// 插入
		pair<iterator, bool> insert(const K &key)
		{
			return _ht.Insert(key);
		}

		// 查找
		iterator find(const K &key)
		{
			return _ht.Find(key);
		}
		
		// 删除
		bool erase(const K &key)
		{
			return _ht.Erase(key);
		}
	};

	// 打印函数
	void print(const unordered_set<int> &s)
	{
		unordered_set<int>::const_iterator it = s.begin();
		while (it != s.end())
		{
			cout << *it << " ";
			++it;
		}
		cout << endl;
	}

	// 测试函数
	void test_unordered_set()
	{
		int a[] = {3, 33, 2, 13, 5, 12, 1002};
		unordered_set<int> s;
		for (auto e : a)
			s.insert(e);

		s.insert(54);
		s.insert(107);

		unordered_set<int>::iterator it = s.begin();
		while (it != s.end())
		{
			cout << *it << " ";
			++it;
		}
		cout << endl;

		for (auto e : s)
			cout << e << " ";

		cout << endl;
		print(s);
	}
}
